package com.goldcn.user.service;

import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.model.DataResult;
import com.goldcn.common.util.CommonUtils;
import com.goldcn.common.util.IdGen;
import com.goldcn.config.ProConstants;
import com.goldcn.user.dao.CompanyDecorationMapper;
import com.goldcn.user.dao.CompanyDecorationPageMapper;
import com.goldcn.user.model.CompanyDecoration;
import com.goldcn.user.model.CompanyDecorationPage;
import com.goldcn.util.HttpUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author Tim Peng
 * mail: lmn_022@163.com
 * Date: 2017/10/23
 * Time: 14:33
 * To change this template use File | Settings | File Templates.
 */
@Service
@Transactional
public class CompanyDecorationPageService {

    @Autowired
    CompanyDecorationPageMapper companyDecorationPageMapper;

    @Autowired
    CompanyDecorationMapper companyDecorationMapper;

    @Autowired
    RestTemplate restTemplate;

    public Object updateDecorationPage(CompanyDecorationPage page){
        CompanyDecoration companyDecoration = companyDecorationMapper.selectByPrimaryKey(page.getDecorateID());
        if(null == companyDecoration){
            throw new ServiceException("店铺信息不存在", RestConst.ErrorCode.FORM_FAIL);
        }
        if(null!=page && CommonUtils.isNotEmpty(page.getSectionKey())){
            page.setUpdateUserId("");
            page.setLastUpdateDate(System.currentTimeMillis());
            if(null == page.getPageID()){
                page.setPageID(IdGen.get().nextId());
                companyDecorationPageMapper.insertSelective(page);
            }else{
                companyDecorationPageMapper.updateByPrimaryKeySelective(page);
            }
           return companyDecorationPageMapper.selectByPrimaryKey(page.getPageID());
        }
        return "error";
    }


    public List<CompanyDecorationPage> selectDecorationPage(Long decorateID){
        List<CompanyDecorationPage> pageList= companyDecorationPageMapper.getListByDecorateID(decorateID);
//        if (null!=pageList && pageList.size()>0) {
//            List<Long> goodsIDList = new ArrayList();
//            for (CompanyDecorationPage page : pageList) {
//                if (null!=page && CommonUtils.isNotEmpty(page.getSectionList())) {
//                    goodsIDList.add(page.getPageID());
//                }
//            }
//
//            if (goodsIDList.size() > 0) {
//                Map<String, Object> price = getGoodsPrice(goodsIDList); //获取商品对应的实时价格
//                for (CompanyDecorationPage page : pageList) {
//                    if (page.getSectionSrcID() != null) {
//                        Object goodsPrice = price.get(page.getSectionSrcID().toString());
//                        if (goodsPrice != null && !goodsPrice.equals("0")) {
//                            JSONObject jsonObject = JSONObject.fromObject(page.getSectionList());
//                            jsonObject.put("price", goodsPrice.toString());
//                            page.setSectionList(jsonObject.toString());
//                            companyDecorationPageMapper.updateByPrimaryKeySelective(page);
//                        }
//                    }
//                }
//            }
//        }
        return pageList;
    }

    private Map<String, Object> getGoodsPrice(List<Long> list) {
        DataResult result = new DataResult();
        result = HttpUtil.httpPost(ProConstants.getGoodsPriceByIDList, list, restTemplate);
        return (Map<String, Object>) result.getData();
    }
}
